Skip to main content

Overview

Creates torchvision transform pipelines for preprocessing images before passing them to CLIP vision encoders. Accepts a PreprocessCfg configuration object for clean, declarative transform configuration.

Function Signature

Parameters

PreprocessCfg
required
Preprocessing configuration object containing:
  • size: Target image size (int or tuple)
  • mean: Normalization mean values
  • std: Normalization std values
  • interpolation: Resize interpolation method
  • resize_mode: How to resize images (‘shortest’, ‘longest’, ‘squash’)
  • fill_color: Padding fill color
See PreprocessCfg for details.
bool
required
Whether to create training (with augmentation) or inference (deterministic) transforms.
Union[Dict, AugmentationCfg]
default:"None"
Augmentation configuration for training. Only used when is_train=True.See AugmentationCfg for options.

Returns

torchvision.transforms.Compose
Composed transform pipeline that can be applied to PIL Images. Includes:
  • Training: Random crops, color jitter, normalization
  • Inference: Resize, center crop, normalization

Examples

Create inference transform

Create training transform with augmentation

Use with model preprocess config

Different resize modes

Training with timm augmentations

Non-square images

Transform Pipeline

Inference Mode (is_train=False)

  1. Resize based on resize_mode:
    • shortest: Resize shortest edge to target size
    • longest: Resize longest edge to target size
    • squash: Resize to exact dimensions
  2. Center Crop (or pad if needed)
  3. Convert to RGB
  4. To Tensor
  5. Normalize with mean/std

Training Mode (is_train=True)

  1. Random Resized Crop with scale and ratio
  2. Convert to RGB
  3. Color Jitter (optional, based on aug_cfg)
  4. Grayscale (optional, based on aug_cfg)
  5. To Tensor
  6. Normalize with mean/std
  7. Random Erasing (optional, if using timm)

Notes

  • For most use cases, use the transforms returned by create_model_and_transforms()
  • PreprocessCfg provides type-safe configuration compared to passing individual parameters
  • Training transforms include random augmentation for better generalization
  • Inference transforms are deterministic and optimized for consistent preprocessing
  • Images are automatically converted to RGB mode
  • Normalization uses ImageNet statistics by default

See Also